﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.IO;

namespace DataTableDataSet {
    /// <summary>
    /// Lógica de interacción para MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {

        private string schema = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Schema.txt";
        private string data = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Data.txt";
        DataSet ds = new DataSet();


        public MainWindow() {
            InitializeComponent();
            populateDataset();
            dataGrid1.DataContext = ds;
            dataGrid1.ItemsSource = ds.Tables[0].Rows;
        }



        public void populateDataset() {
            if (File.Exists(schema)) {
                ds.ReadXmlSchema(schema);
            } else { createSchema(); }

            if (File.Exists(data)) {
                ds.ReadXml(data, XmlReadMode.IgnoreSchema);
            } 
        
        }

        public void createSchema() {
            var dt1 = ds.Tables.Add("t1");
            var c1 = dt1.Columns.Add("id", typeof(String));
            c1.ColumnMapping = MappingType.Attribute;
            var c2 = dt1.Columns.Add("name", typeof(String));
            c2.ColumnMapping = MappingType.Attribute;
            FileStream fs = new FileStream(schema, FileMode.OpenOrCreate);
            dt1.WriteXmlSchema(fs);
            dt1.Rows.Add("1", "jose");
            dt1.Rows.Add("2", "luis");
            fs = new FileStream(data, FileMode.OpenOrCreate);
            ds.WriteXml(fs);
            fs.Close();
        }
    }
}
